<?php

use yii\helpers\Url;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use website\helpers\Render;
use common\models\User;
use common\models\Design;
use common\models\Category;
use common\models\UserUpload;

$this->addCrumbs('个人设置');
$this->title = '基本资料修改';
?>

<div class="contenter">
    <?= $this->render('/user/navigator', ['active' => 'informationt']) ?>
    <div id="user-contenter">
        <div class="box-shadow" id="sub-contenter">
            <div class="data-title"><span class="h3"><?= $this->title ?></span></div>
            <div class="data-content flyer-form">
                <form class="mt-20px" id="flyer-create" enctype="multipart/form-data" onsubmit="return false;">
                    <div class="form-item checker">
                        <div class="input-title">昵称：</div>
                        <div class="input-inline"><input class="flyer-input" name="nickname" type="text" value="<?= $user->nickname ?>"></div>
                    </div>
                    <div class="form-item checker">
                        <div class="input-title">月薪：</div>
                        <div class="input-inline"><input class="flyer-input" name="salary" type="text" value="<?= $user->salary ?>"></div>
                    </div>
                    <div class="form-item checker">
                        <div class="input-title">QQ：</div>
                        <div class="input-inline"><input class="flyer-input" name="qq" type="text" value="<?= $user->qq ?>"></div>
                    </div>
                    <div class="form-item checker">
                        <div class="input-title">工作年限：</div>
                        <div class="input-block flyer-choise-conter moderate red-choise"><?= Render::radio('work_life', User::$workLifeSelector, $user->work_life) ?></div>
                    </div>
                    <div class="form-item checker">
                        <div class="input-title">擅长类目：</div>
                        <div class="input-block flyer-choise-conter moderate red-choise"><?= Render::checkbox('category_id', $categories, $user->categoriesArray, [], ['value' => 'id']) ?></div>
                    </div>
                    <div class="form-item checker">
                        <div class="input-title">擅长风格：</div>
                        <div class="input-block flyer-choise-conter moderate red-choise"><?= Render::checkbox('style', Design::$styleSelector, $user->stylesArray) ?></div>
                    </div>
                    <div class="form-item checker">
                        <div class="input-title">擅长类型：</div>
                        <div class="input-block flyer-choise-conter moderate red-choise"><?= Render::checkbox('model', $models, $user->modelsArray, [], ['value' => 'id']) ?></div>
                    </div>
                    <div class="form-item checker">
                        <div class="input-title">擅长软件：</div>
                        <div class="input-block"><input class="flyer-input" name="skills" type="text" value="<?= $user->skills ?>"></div>
                        <input class="flyer-input" name="status" type="hidden" value="1">
                    </div>
                    <div class="form-item checker">
                        <div class="input-title">标签：</div>
                        <div class="input-block"><input class="flyer-input" name="tags" type="text" value="<?= $user->tags ?>"></div>
                    </div>
                    <div class="form-item checker">
                        <div class="input-title">个人描述：</div>
                        <div class="input-block"><textarea class="flyer-textarea" name="describe"><?= $user->describe ?></textarea></div>
                    </div>

                    <div class="form-item checker">
                        <div class="input-title"></div>
                        <div class="input-inline"><button class="flyer-button normal border-round" id="save-button" type="submit">修改提交</button></div>
                    </div>
                    <textarea id="flyer-create-json" data-form="#flyer-create" style="display:none;"><?= User::checker('informationt') ?></textarea>
                    <input type="hidden" name="_csrf" value="<?= Yii::$app->request->getCsrfToken() ?>">

                </form>
            </div>
        </div>
    </div>
</div>

<script src="<?= Render::static('flyer/flyer.class.js') ?>"></script>
<script src="<?= Render::static('flyer/checker.class.js') ?>"></script>
<script src="<?= Render::static('flyer/tableHandler.class.js') ?>"></script>
<!-- 上传图片类 -->
<link href="<?= Render::static('uploader/loader.css') ?>" rel="stylesheet">
<script src="<?= Render::static('uploader/loader.core.single.class.js') ?>"></script>
<script src="<?= Render::static('uploader/loader.preview.class.js') ?>"></script>
<script>
    var checkerClass = new checker();
    $(document).ready(function() {
        // 表单美化
        (new flyer).init({ form: '#flyer-create' });

        if($('.designs .design-one').length) {
            $.each($('.designs .design-one'), function(index) {
                var designs = $.parseJSON('<?= $designs ?>');
                (new loaderPerview).init({
                    conter: this, name: 'design' + index, maxSize: 1024 * 1024 * 4,
                    prevLoaders: designs[index] ? designs[index] : ''
                });
            });
        }

        // 表单数据验证
        checkerClass.init({ ruleDom: '#flyer-create-json' });

        // 多选栏目选择数量控制
        var checkboxLimit = ['category_id', 'style', 'model'];
        $.each(checkboxLimit, function(index) {
            var name = checkboxLimit[index];
            var currentEvent = "input[name='" + name + "[]']";
            $(currentEvent).bind('change', function() {
                var checkboxs = "input[name='" + $(this).attr('name') + "']";
                if($(checkboxs + ':checked').length > 3) {
                    $(this).removeAttr('checked');
                    layer.msg('最多只能选择3个');
                    return false;
                }
                return true;
            });
        });

        $('#save-button').bind('click', function() {
            if(checkerClass.validate()) {
                $('#save-button').attr('disabled', true).html('保存中');
                $.ajax({
                    url: "<?= Url::to('@web/designer/informationt') ?>",
                    cache: false,
                    data: new FormData($('#flyer-create')[0]),
                    type: 'POST',
                    processData: false,
                    contentType: false,
                    dataType:"json",
                    success: function(response) {
                        if(response.code == 200) {
                            layer.alert('资料保存成功', { icon: 6 }, function() {
                                window.location.href = "<?= Url::to('@web/designer/resume') ?>";
                            });
                        }
                        else {
                            layer.msg(response.message, { shift: 6 });
                        }
                        $('#save-button').removeAttr('disabled').html('保存');
                    }
                });
            }
        });
    });
</script>